# -*- coding: utf-8 -*-
import scrapy
import json
from scrapy.crawler import CrawlerProcess
from scrapy.utils.project import get_project_settings


class MafengwoSpider(scrapy.Spider):
    name = 'mafengwo'
    allowed_domains = ['m.mafengwo.cn']
    start_urls = ['https://m.mafengwo.cn/hotel/10099/']

    def start_requests(self):
        cookies_str = 'PHPSESSID=2v3ud1jpktj3k5he0hlhtcasg2; mfw_uuid=5de0ce2c-91ae-dace-141a-a752e2199f61; oad_n=a%3A3%3A%7Bs%3A3%3A%22oid%22%3Bi%3A1029%3Bs%3A2%3A%22dm%22%3Bs%3A15%3A%22www.mafengwo.cn%22%3Bs%3A2%3A%22ft%22%3Bs%3A19%3A%222019-11-29+15%3A52%3A12%22%3B%7D; __omc_chl=; __omc_r=; __mfwc=direct; __mfwlv=1575013932; __mfwvn=1; uva=s%3A78%3A%22a%3A3%3A%7Bs%3A2%3A%22lt%22%3Bi%3A1575013933%3Bs%3A10%3A%22last_refer%22%3Bs%3A6%3A%22direct%22%3Bs%3A5%3A%22rhost%22%3Bs%3A0%3A%22%22%3B%7D%22%3B; __mfwurd=a%3A3%3A%7Bs%3A6%3A%22f_time%22%3Bi%3A1575013933%3Bs%3A9%3A%22f_rdomain%22%3Bs%3A0%3A%22%22%3Bs%3A6%3A%22f_host%22%3Bs%3A3%3A%22www%22%3B%7D; __mfwuuid=5de0ce2c-91ae-dace-141a-a752e2199f61; Hm_lvt_8288b2ed37e5bc9b4c9f7008798d2de0=1575013933; UM_distinctid=16eb625645db7f-0355fbb2cc8e49-e353165-1fa400-16eb625645e1cc; __mfwa=1575013932780.22447.2.1575013932780.1575016823199; __jsluid_h=ef0f1ab446fb7bcbd73b1816ed272af6; login=mafengwo; mafengwo=e39b1349ba1fb215fbd73f31fe2aed0b_74561635_5de0e087e36de2.32079498_5de0e087e36e13.75483988; uol_throttle=74561635; mfw_uid=74561635; CNZZDATA30065558=cnzz_eid%3D1946913226-1575011712-%26ntime%3D1575014665; Hm_lpvt_8288b2ed37e5bc9b4c9f7008798d2de0=1575019006; __mfwb=c10d5ecc605f.28.direct; __mfwlt=1575019007'
        cookies_dict = {i.split('=')[0]: i.split('=')[1] for i in cookies_str.split('; ')}
        yield scrapy.Request(
                url="http://www.mafengwo.cn/hotel/12522/",
                callback=self.parse,
                headers={
                    'Referer': 'https://hotels.ctrip.com/hotel/chongqing4',
                    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36'
                },
                cookies=cookies_dict,
           )

    def parse(self, response):
        datas = response.body.decode("utf-8")
        # datas = json.loads(response.body_as_unicode())["hotelPositionJSON"]
        # datas = json.dumps(response.body.decode("utf-8")['hotelPositionJSON'], sort_keys=True, ensure_ascii=False, indent=4,separators=(',', ':'))
        # Datas = json.dumps(json.loads(response.body.decode("utf-8"))["hotelPositionJSON"], sort_keys=True, ensure_ascii=False, indent=4,separators=(',', ':'))
        print(datas)


if __name__ == '__main__':
    process = CrawlerProcess(get_project_settings())
    process.crawl('mafengwo')
    process.start()
